package com.pinusmusic.web.serlvet;


import com.alibaba.druid.support.spring.stat.SpringStatUtils;
import com.pinusmusic.domain.*;
import com.pinusmusic.service.MusicListService;
import com.pinusmusic.service.UserService;
import com.pinusmusic.service.impl.MusicListServiceImpl;
import com.pinusmusic.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import javax.swing.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

@WebServlet("/musicList/*")
public class MusicListServlet extends BaseServlet {
    private MusicListService musicListService =  new MusicListServiceImpl();
    private UserService userService =new UserServiceImpl();
    public void addMusicList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        MusicList addMusicList = musicListService.getAddMusicList(request);
        System.out.println(addMusicList);//
        if (addMusicList != null) {
            musicListService.addMusicList(addMusicList);
            response.sendRedirect(request.getContextPath() + "/adminHtml/musicList_list.html");
        }
    }
    public void deleteMusicListAndMusicByMlid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String mlid = request.getParameter("mlid");
        musicListService.deleteMusicListAndMusicByMlid(Integer.parseInt(mlid));
    }
    public void updateMusicList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ResultInfo info = musicListService.getUpdateMusicList(request);
        if (info.isFlag()){
            response.sendRedirect(request.getContextPath()+"/adminHtml/musicList_list.html");
        }
        writeValue(response,info);
    }
    public void findMusicListByPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取当前页码数
        String curStr = request.getParameter("currentPage");
        if (curStr==null || curStr.equals("") || curStr.equals("0")) curStr="1";
        int currentPage = Integer.parseInt(curStr);
        //设置每页显示数据
        int rows = 4;
        PageBean<MusicList> pageBean = musicListService.findMusicListByPage(currentPage,rows);
        writeValue(response,pageBean);
    }
    public void getOne(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String mlid = request.getParameter("mlid");
        MusicList updateMusicList = musicListService.findMusicListByMlid(Integer.parseInt(mlid));
        request.getSession().setAttribute("musicList",updateMusicList);
        writeValue(response,updateMusicList);
    }
    public void deleteMoreMusicList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String[] mlids = request.getParameterValues("mlids");
        musicListService.deleteMoreMusicList(mlids);
    }
    public void addMusic(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String mid = request.getParameter("mid");
        System.out.println("获取到的歌曲id:"+mid);
        MusicList musicList = (MusicList) request.getSession().getAttribute("musicList");
        musicListService.addMusic(musicList.getMlid(),Integer.parseInt(mid));
    }
    public void addMoreMusicToList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String[] mids = request.getParameterValues("mids");
        MusicList musicList = (MusicList) request.getSession().getAttribute("musicList");
        musicListService.addMoreMusicToList(mids,musicList.getMlid());
    }
    public void remMusic(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String mid = request.getParameter("mid");
        System.out.println("获取到的歌曲id:"+mid);
        MusicList musicList = (MusicList) request.getSession().getAttribute("musicList");
        musicListService.remMusic(musicList.getMlid(),Integer.parseInt(mid));
    }
    public void remMoreMusic(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String[] mids = request.getParameterValues("mids");
        MusicList musicList = (MusicList) request.getSession().getAttribute("musicList");
        musicListService.remMoreMusic(mids,musicList.getMlid());
    }
    //歌单的模模糊查询
    public void findMusicListsByName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        System.out.println("MusicList获取的name:"+name);
        //获取前端得到的当前页面
        String curStr = request.getParameter("currentPage");
        //一开始就为第一页
        if (curStr==null || curStr.equals("") || curStr.equals("0")) curStr="1";
        int currentPage = Integer.parseInt(curStr);
        //设置每次查询五条;
        int rows = 5;
        PageBean<MusicList> pageBean = musicListService.findMusicListsByName(currentPage, rows, name);
        System.out.println("findMusicListsByName的结果:"+pageBean);
        writeValue(response,pageBean);
    }

    //2.0
    public void getMusiclistAndCreator(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String mlid = request.getParameter("mlid");
        MusicList updateMusicList = musicListService.findMusiclistAndCreatorByMlid(Integer.parseInt(mlid));
        request.getSession().setAttribute("musicList",updateMusicList);
        writeValue(response,updateMusicList);
    }
    public void getCreationlistByUid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        List<User> users = (List<User>) req.getSession().getAttribute("user");
        System.out.println("从req获取的user:"+users);
        List<MusicList> musiclists=musicListService.findCreationlistByUid(users.get(0).getUid());
        for (MusicList musiclist:musiclists) {
            musiclist.setUser(users.get(0));
        }
        System.out.println("获取到的歌单list:"+musiclists);
        this.writeValue(resp,musiclists);
    }
    public void deleteByMlid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String mlid = request.getParameter("mlid");
        musicListService.deleteMusicListAndMusicByMlid(Integer.parseInt(mlid));
    }
    public void deletemusiclistsByMlid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String mlids = request.getParameter("mlids");
        musicListService.deleteMusicListAndMusicByMlid(Integer.parseInt(mlids));
    }
    public void getCollectedlistByUid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        List<User> users = (List<User>) req.getSession().getAttribute("user");
        System.out.println("从req获取的user:"+users);
        List<MusicList> musiclists=musicListService.findCollectedlistByUid(users.get(0).getUid());
        for (MusicList musiclist:musiclists) {
            musiclist.setUser(userService.findUserByUid(musiclist.getUser().getUid()));
        }
//        System.out.println("servlet获取到的歌单list:"+musiclists);
        this.writeValue(resp,musiclists);
    }
    public void cancelCollectByMlid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String mlid = req.getParameter("mlid");
        int uid = (int) req.getSession().getAttribute("uid");
        System.out.println(uid);
        musicListService.cancelCollectByMlid(Integer.parseInt(mlid),uid);
    }
    public void userAddList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String mid = req.getParameter("mid");
        String musiclistname = req.getParameter("musiclistname");
        System.out.println("音乐id:"+mid+",歌单名："+musiclistname);
        List<User> user = (List<User>) req.getSession().getAttribute("user");
        User loginUser = user.get(0);
        boolean flag = musicListService.createListAndAdd(musiclistname,loginUser.getUid(),Integer.parseInt(mid));
        writeValue(resp,flag);
    }
    public void collectMusiclist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String mlid = req.getParameter("mlid");
        int uid = (int) req.getSession().getAttribute("uid");
        System.out.println("uid:"+uid);
        System.out.println("mlid:"+mlid);
        musicListService.collectByMlid(Integer.parseInt(mlid),uid);
        resp.sendRedirect(req.getContextPath()+"/userhtml/musicListInfo.html?mlid="+mlid);
    }

    //查找其他用户的收藏歌单
    public void getsbCollectedlistByUid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int uid=Integer.parseInt(req.getParameter("uid"));
        System.out.println("从req获取的uid:"+uid);
        List<MusicList> musiclists=musicListService.findCollectedlistByUid(uid);
        for (MusicList musiclist:musiclists) {
            musiclist.setUser(userService.findUserByUid(musiclist.getUser().getUid()));
        }
//        System.out.println("servlet获取到的歌单list:"+musiclists);
        this.writeValue(resp,musiclists);
    }
    //查找其他用户创建的歌单
    public void getsbCreationlistByUid(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        int uid=Integer.parseInt(req.getParameter("uid"));
        System.out.println("从req获取的uid:"+uid);
        User sb=userService.findUserByUid(uid);
        List<MusicList> musiclists=musicListService.findCreationlistByUid(uid);
        for (MusicList musiclist:musiclists) {
            musiclist.setUser(sb);
        }
        System.out.println("获取到的歌单list:"+musiclists);
        this.writeValue(resp,musiclists);
    }
}
